$include("$theme/feedListView.css");


$import("fp.jq.Scrollable");
$import("fp.renderers.FeedDetailRenderer");
$import("fp.views.BaseFeedView");


with ($ns("fp.views"))
{
    fp.views.FeedListView = function($p_container, p_options)
    {
        var me = new fp.views.BaseFeedView($p_container, p_options)
        var base = {};
        
        me.viewType = "list";
        me.renderer = new fp.renderers.FeedDetailRenderer();
        
        var _$feedContainer = null;
        var _$feeds = [];
        
        base.init = me.init;
        me.init = function()
        {           
            base.init();
            
            _$feedContainer = $("<div id='feedContainer'/>");
            me.$container.append(_$feedContainer);
            me.$container.verticalScrollable();
            
            me.$container.addClass("feedListView");
            me.$container.bind("scroll", _onscroll);
        };
        
        
        me.renderFeed = function(p_feed)
        {
            var $feed = me.renderer.renderFeed(p_feed);
            _$feedContainer.append($feed);
            _$feeds.add($feed);
        };
        
        
        base.appendFeeds = me.appendFeeds; 
        me.appendFeeds = function(p_feeds)
        {
            base.appendFeeds(p_feeds);
            me.$container.updateScrollbars();
        }

        
        base.clearFeeds = me.clearFeeds;
        me.clearFeeds = function()
        {
            base.clearFeeds();
            for (var i = 0; i < _$feeds.length; i++)
            {
                _$feeds[i].remove();
            }
            _$feeds.clear();
            me.$container.updateScrollbars();
        };
        
        
        me.moveNext = function()
        {
            var $feed = _getCurrentFeed();
            
            if ($feed != null)
            {
                var $nextFeed = $feed.next();
                if ($nextFeed.length > 0)
                {
                    var nfTop = $nextFeed.offset().top - 50 + me.$container.scrollTop() - 18;
                    me.$container.stop(true, true);
                    me.$container.animate({ scrollTop: nfTop }, 200, "linear");
                }
            }
        };
        
        me.movePrevious = function()
        {
            var $feed = _getCurrentFeed();
            
            if ($feed != null)
            {
                var $prevFeed = $feed.prev();
                if ($prevFeed.length > 0)
                {
                    var pfTop = $prevFeed.offset().top - 50 + me.$container.scrollTop() - 18;
                    me.$container.stop(true, true);
                    me.$container.animate({ scrollTop: pfTop }, 100, "linear");
                }
            }
        };
        
        me.openInNewTab = function()
        {
            var $feed = _getCurrentFeed();
            
            if ($feed != null)
            {
                var feed = $feed.data("feed");
                if (feed != null && feed.alternate != null && feed.alternate.length > 0)
                {
                    window.open(feed.alternate[0].href);
                }
            }
        };
        
        me.toggleStar = function()
        {
            var $feed = _getCurrentFeed();
            
            if ($feed != null)
            {
                $feed.find("#star").click();
            }
        };
        
        
        function _getCurrentFeed()
        {
            var sTop = me.$container.scrollTop();
            for (var i = 0; i < _$feeds.length; i++)
            {
                var $feed = _$feeds[i];
                var fTop = $feed.offset().top - 50 + sTop;
                var fBottom = fTop + $feed.outerHeight();
                if (fBottom > sTop)
                {
                    return $feed;
                }
                else if (fTop >= sTop)
                {
                    return $feed; 
                }
            }
            return null;
        }
        
        
        
        
        function _onscroll()
        {
            if (me.$container.height() + me.$container.scrollTop() > _$feedContainer.height() - _$feeds[_$feeds.length - 1].outerHeight() - me.$container.height())
            {
                me.trigger("loadingrequired");
            }
        }
        
        return me;
    }
}